package com.bw.a2207a.view.fragment

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import com.alibaba.android.arouter.launcher.ARouter
import com.alipay.sdk.app.PayTask
import com.bw.a2207a.R
import com.bw.a2207a.base.BaseFragment
import com.bw.a2207a.bean.CommentBean
import com.bw.a2207a.bean.GoodsBean
import com.bw.a2207a.databinding.FragmentGoodsDetailBinding
import com.bw.a2207a.util.GlideUtil
import com.bw.a2207a.view.VideoActivity
import com.bw.a2207a.view.adapter.CommentAdapter
import com.bw.a2207a.viewmodel.GoodsDetailViewModel
import com.stx.xhb.androidx.XBanner

/**
 * create an instance of this fragment.
 */
class GoodsDetailFragment :
    BaseFragment<FragmentGoodsDetailBinding, GoodsDetailViewModel>(R.layout.fragment_goods_detail) {
    lateinit var adapter: CommentAdapter
    lateinit var goodsBean: GoodsBean
    val datas = mutableListOf<CommentBean>()
    override fun initData() {
        viewModel.commentBeanLiveData.observe(this) {
            if (it.code == 200) {
                datas.clear()
                datas.addAll(it.data)
                adapter.notifyDataSetChanged()

            }
        }

        val banners = mutableListOf<String>()
        //设置商品banner
        viewModel.goodsLiveData.observe(this) {

            goodsBean = it.data

            banners.add("http://ali2.a.yximgs.com/upic/2020/09/02/22/BMjAyMDA5MDIyMjAwNDZfNjU0Njc0XzM1NDM5MTg2Mjg3XzFfMw==_low_B1d223c4240dff71a58d062c26331ab9e.webp?tag=1-1599265874-vdg-0-w10q14bbdx-24bc3d6488ad4b86&bp=10000")
            banners.addAll(it.data.bannerList)

            binding.detailBanner.setData(banners, null)
            binding.detailBanner.setmAdapter { banner, model, view, position ->
                GlideUtil.loadImg(
                    requireContext(),
                    banners.get(position),
                    view as ImageView
                )
            }
            //设置banner点击事件
            binding.detailBanner.setOnItemClickListener(object : XBanner.OnItemClickListener {
                override fun onItemClick(
                    banner: XBanner?,
                    model: Any?,
                    view: View?,
                    position: Int
                ) {

                    //跳转到视频播放页面
                    startActivity(Intent(activity, VideoActivity::class.java))
                }

            })

            //设置价格
            binding.tvPrice.text = "¥ ${it.data.goods_default_price}"
            //设置商品描述信息
            binding.tvDesc.text = it.data.goods_desc
        }
    }

    override fun initView() {
        val id = arguments?.getInt("id")
        //获取评论列表
        id?.let { viewModel.getGoodsDetail(it) }

        binding.tvBuy.setOnClickListener {
            val orderInfo: String =
                "app_id=2015052600090779&biz_content=%7B%22timeout_expres" // 订单信息

            val payRunnable = Runnable {
                val alipay = PayTask(activity)
                val result = alipay.payV2(orderInfo, true)
                if (result != null) {
                    //支持成功，跳转页面
                    ARouter.getInstance()
                        .build("/main/PayResultActivity")
                        .withInt("price", goodsBean.goods_default_price)
                        .withString("desc", goodsBean.goods_desc)
                        .withString("url", goodsBean.bannerList.get(0))
                        .navigation()
                    //startActivity(Intent(activity, PayResultActivity::class.java))
                }
            }
            // 必须异步调用
            // 必须异步调用
            val payThread = Thread(payRunnable)
            payThread.start()
        }

//        //左侧分类列表
//        binding.rvComment.layoutManager = LinearLayoutManager(activity)
//        adapter = CommentAdapter(activity, R.layout.item_comment_layout, datas)
//        binding.rvComment.adapter = adapter

        binding.tv1.setOnClickListener {
            binding.likeview.addLikeImage(R.drawable.ic_favorite_24)
            binding.likeview.addFavor()
        }

    }

    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment BlankFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(id: Int) =
            GoodsDetailFragment().apply {
                arguments = Bundle().apply {
                    putInt("id", id)
                }
            }
    }


}